package org.jeecg.modules.car.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.entity.car.CarDerateDetails;
import org.jeecg.entity.car.CarMerchantDerate;
import org.jeecg.enums.car.CarDerateDetailStatusEnum;
import org.jeecg.enums.car.CarDerateStatusEnum;
import org.jeecg.model.car.reqvo.CarDayReportReqVo;
import org.jeecg.model.car.respvo.CarDayReportRespVo;
import org.jeecg.modules.car.mapper.CarDerateDetailsMapper;
import org.jeecg.modules.car.mapper.CarMerchantDerateMapper;
import org.jeecg.modules.car.service.ICarDerateDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.ServerSocket;
import java.util.List;

/**
 * @Description: 优惠卷明细
 * @Author: jeecg-boot
 * @Date:   2020-11-08
 * @Version: V1.0
 */
@Service
public class CarDerateDetailsServiceImpl extends ServiceImpl<CarDerateDetailsMapper, CarDerateDetails> implements ICarDerateDetailsService {

    @Autowired
    private CarMerchantDerateMapper carMerchantDerateMapper;
    @Autowired
    private CarDerateDetailsMapper carDerateDetailsMapper;

    @Override
    @Transactional
    public Result<String> addDetails(CarMerchantDerate carMerchantDerate, String plate) {
        CarDerateDetails derateDetails = new CarDerateDetails();
        try {
            derateDetails.setTenantId(carMerchantDerate.getTenantId());
            derateDetails.setProjectId(carMerchantDerate.getProjectId());
            derateDetails.setDerateId(carMerchantDerate.getId());
            derateDetails.setDerateAmount(carMerchantDerate.getDerateAmount());
            derateDetails.setCreateBy(carMerchantDerate.getCreateBy());
            derateDetails.setUpdateBy(carMerchantDerate.getUpdateBy());
            derateDetails.setPlateNumber(plate);
            //状态  已绑定
            derateDetails.setStatus(CarDerateDetailStatusEnum.CAR_DERATE_DETAIL_STATUS_0.getCode());
            //添加优惠卷明细
            this.save(derateDetails);
            //判断优惠卷是否为最后一次使用
            if (carMerchantDerate.getUsableNumber() - carMerchantDerate.getUsedNumber() > 1){
                //更新优惠卷信息  已使用次数+1
                carMerchantDerateMapper.updateUseStatus(carMerchantDerate.getId(), CarDerateStatusEnum.CAR_DERATE_STATUS_0.getCode(),CarDerateStatusEnum.CAR_DERATE_STATUS_0.getCode(),carMerchantDerate.getUsedNumber()+1);
            }else {
                //更新优惠卷信息  已使用次数+1 && 状态改为已使用
                carMerchantDerateMapper.updateUseStatus(carMerchantDerate.getId(), CarDerateStatusEnum.CAR_DERATE_STATUS_0.getCode(),CarDerateStatusEnum.CAR_DERATE_STATUS_2.getCode(),carMerchantDerate.getUsedNumber()+1);
            }
        }catch (Exception e){
            log.error("优惠卷绑定车牌异常:{}",e);
            return Result.error("处理异常");
        }
        return Result.ok(derateDetails.getId());
    }

    /**
     * 根据优惠卷  车牌获取车辆下是否已经绑定该优惠卷
     * @param plateNumber
     * @return
     */
    @Override
    public CarDerateDetails getDerateDetail(String plateNumber) {
        return carDerateDetailsMapper.getDerateDetail(plateNumber);
    }

    @Override
    public List<CarDerateDetails> findDetailPage(Page<CarDerateDetails> page, CarDerateDetails carDerateDetails) {
        carDerateDetails.setTenantId(SsoUtil.getLoginUser().getTenantId());
        return carDerateDetailsMapper.findDetailPage(page,carDerateDetails);
    }

    @Override
    public List<CarDayReportRespVo> findGroupList(CarDayReportReqVo carDayReportReqVo) {
        return carDerateDetailsMapper.findGroupList(carDayReportReqVo);
    }
}
